package com.drondea.testclient.util.counter;

import cn.hutool.core.util.StrUtil;
import com.drondea.testclient.panel.SgipPanel;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 计数静态类
 * @author 1
 */
@Slf4j
public class SgipAtomicUtil {

    private static final SgipPanel PANEL = SgipPanel.getInstance();

    public static final AtomicLong SEND_COUNT = new AtomicLong();
    public static final AtomicLong RESPONSE_COUNT = new AtomicLong();
    public static final AtomicLong SUCCESS_COUNT = new AtomicLong();
    public static final AtomicLong FAIL_COUNT = new AtomicLong();
    public static final AtomicLong REPORT_COUNT = new AtomicLong();

    /**
     * 延迟大于50MS
     */
    public static final AtomicLong DELAY_GT50MS_COUNT = new AtomicLong(0);

    /**
     * 延迟大于100MS
     */
    public static final AtomicLong DELAY_GT100MS_COUNT = new AtomicLong(0);

    /**
     * 延迟大于500MS
     */
    public static final AtomicLong DELAY_GT500MS_COUNT = new AtomicLong(0);

    /**
     * 延迟大于1秒
     */
    public static final AtomicLong DELAY_GT1S_COUNT = new AtomicLong(0);

    public static void handlerDelay(long delay) {
        if (delay > 1000) {
            PANEL.delayGt1sCountLabel.setText(StrUtil.format("响应延迟大于1s: {}", DELAY_GT1S_COUNT.incrementAndGet()));
        } else if (delay > 500) {
            PANEL.delayGt500msCountLabel.setText(StrUtil.format("响应延迟大于500ms: {}", DELAY_GT500MS_COUNT.incrementAndGet()));
        } else if (delay > 100) {
            PANEL.delayGt100msCountLabel.setText(StrUtil.format("响应延迟大于100ms: {}", DELAY_GT100MS_COUNT.incrementAndGet()));
        } else if (delay > 50) {
            PANEL.delayGt50msCountLabel.setText(StrUtil.format("响应延迟大于50ms: {}", DELAY_GT50MS_COUNT.incrementAndGet()));
        }
    }


    public static void clear() {
        SEND_COUNT.set(0);
        RESPONSE_COUNT.set(0);
        SUCCESS_COUNT.set(0);
        FAIL_COUNT.set(0);
        REPORT_COUNT.set(0);

        DELAY_GT50MS_COUNT.set(0);
        DELAY_GT100MS_COUNT.set(0);
        DELAY_GT500MS_COUNT.set(0);
        DELAY_GT1S_COUNT.set(0);
    }
}
